//index.js
//获取应用实例
// var WxSearch = require('../../wxSearch/wxSearch.js')
var common = require('../../utils/common.js')
var Bmob = require("../../utils/bmob.js");
var util = require('../../utils/util.js');
var app = getApp()
var that;
var smoodList;
Page({
  data: {
    buttonClicked: false, //是否点击跳转
    tradeType: 0,
    moodList: [],
    isEmpty: true,
    loading: false
  },
  //选择要查询的活动类型
  choseTradeType: function(e) {
    var tradeType = e.currentTarget.id;
    if (tradeType == 0) this.onShow();
    else if (tradeType == 1) this.setData({
      moodList: this.data.sportList
    });
    else if (tradeType == 2) this.setData({
      moodList: this.data.gameList
    });
    else if (tradeType == 3) this.setData({
      moodList: this.data.friendList
    });
    else if (tradeType == 4) this.setData({
      moodList: this.data.travelList
    });
    else if (tradeType == 5) this.setData({
      moodList: this.data.readList
    });
    else if (tradeType == 6) this.setData({
      moodList: this.data.contestlist
    });
    else if (tradeType == 7) this.setData({
      moodList: this.data.movieList
    });
    else if (tradeType == 8) this.setData({
      moodList: this.data.musicList
    });
    else if (tradeType == 9) this.setData({
      moodList: this.data.otherList
    });
    this.setData({
      tradeType: tradeType
    })
  },
  onLoad: function() {

  },

  onShow: function() {
    that = this
    var molist = new Array();
    var Diary = Bmob.Object.extend("Events");
    var query = new Bmob.Query(Diary);
    query.equalTo("isShow", 1); //只统计公开显示的活动
    query.descending("createdAt");
    query.include("publisher");
    // 查询所有数据
    query.find({
      success: function(results) {
        for (var i = 0; i < results.length; i++) {
          var publisherId = results[i].get("publisher").objectId;
          var title = results[i].get("title");
          var content = results[i].get("content");
          var acttype = results[i].get("acttype");
          var isShow = results[i].get("isShow");
          var endtime = results[i].get("endtime");
          var address = results[i].get("address");
          var acttypename = getTypeName(acttype);
          var peoplenum = results[i].get("peoplenum");
          var likenum = results[i].get("likenum");
          var liker = results[i].get("liker");
          var isLike = 0;
          var commentnum = results[i].get("commentnum");
          var id = results[i].id;
          var createdAt = results[i].createdAt;
          var pubtime = util.getDateDiff(createdAt);
          var _url
          var actpic = results[i].get("actpic");
          if (actpic) {
            _url = results[i].get("actpic");
          } else {
            _url = "/static/images/base.png";
          }
          var publisherName = results[i].get("publisher").nickname;
          var publisherPic = results[i].get("publisher").userPic;
          var jsonA;
          jsonA = {
            "title": title || '',
            "content": content || '',
            "acttype": acttype || '',
            "isShow": isShow,
            "acttypename": acttypename || '',
            "endtime": endtime || '',
            "address": address || '',
            "peoplenum": peoplenum || '',
            "id": id || '',
            "avatarUrl": publisherPic || '',
            "nickName": publisherName || '',
            "publisherId": publisherId || '',
            "pubtime": pubtime || '',
            "actpic": _url || '',
            "likenum": likenum,
            "commentnum": commentnum,
            "is_liked": isLike || ''
          }
          molist.push(jsonA);
          smoodList = molist;
          var sportlist = new Array(); //运动
          var gamelist = new Array(); //游戏
          var friendlist = new Array(); //交友
          var travellist = new Array(); //旅行
          var readlist = new Array(); //读书
          var contestlist = new Array(); //竞赛
          var movielist = new Array(); //电影
          var musiclist = new Array(); //音乐
          var otherlist = new Array(); //其他
          for (var i in molist) {
            if (molist[i].acttype == 1) sportlist.push(molist[i]);
            else if (molist[i].acttype == 2) gamelist.push(molist[i]);
            else if (molist[i].acttype == 3) friendlist.push(molist[i]);
            else if (molist[i].acttype == 4) travellist.push(molist[i]);
            else if (molist[i].acttype == 5) readlist.push(molist[i]);
            else if (molist[i].acttype == 6) contestlist.push(molist[i]);
            else if (molist[i].acttype == 7) movielist.push(molist[i]);
            else if (molist[i].acttype == 8) musiclist.push(molist[i]);
            else if (molist[i].acttype == 9) otherlist.push(molist[i]);
          }
          that.setData({
            moodList: molist,
            sportList: sportlist,
            gameList: gamelist,
            friendList: friendlist,
            travelList: travellist,
            readList: readlist,
            contestlist: contestlist,
            movieList: movielist,
            musicList: musiclist,
            otherList: otherlist,
            loading: true
          })
        }
      },
      error: function(error) {
        common.dataLoading(error, "loading");
        console.log(error)
      }
    });
  },




  // 点击活动进入活动详情页面
  click_activity: function(e) {
    console.log(getCurrentPages())
    if (!this.buttonClicked) {
      util.buttonClicked(this);
      let actid = e.currentTarget.dataset.actid;
      let pubid = e.currentTarget.dataset.pubid;
      // let user_key = wx.getStorageSync('user_key');
      wx.navigateTo({
        url: '/pages/detail/detail?actid=' + actid + "&pubid=" + pubid
      });
    }
  },
  //--------------------------------------------------------


  //
  wxSearchInput: function(e) {
    //云开发正则模糊查询
    //三字段 标题 内容 地点
    //按创建时间降序..新创建的在前
    that = this
    const value = e.detail.value
    const db = wx.cloud.database()
    const _ = db.command
    db.collection('Events')
    .orderBy('createTime', 'desc')
      .where(_.or([
          {
          title: db.RegExp({
            regexp: '.*' + value,
            options: 'i',
          })
        },
        {
          content: db.RegExp({
            regexp: '.*' + value,
            options: 'i',
          })
        },
        {
          address: db.RegExp({
            regexp: '.*' + value,
            options: 'i',
          })
        }
      ]))
      .get()
      .then(res => {
        console.log(res, 7777)
        that.setData({
          moodList: res.data
        })
      })
  }

})

//根据活动类型获取活动类型名称
function getTypeName(acttype) {
  var acttypeName = "";
  if (acttype == 1) acttypeName = "运动";
  else if (acttype == 2) acttypeName = "游戏";
  else if (acttype == 3) acttypeName = "交友";
  else if (acttype == 4) acttypeName = "旅行";
  else if (acttype == 5) acttypeName = "读书";
  else if (acttype == 6) acttypeName = "竞赛";
  else if (acttype == 7) acttypeName = "电影";
  else if (acttype == 8) acttypeName = "音乐";
  else if (acttype == 9) acttypeName = "其他";
  return acttypeName;
}
